package com.zidian.study.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;


import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;
import com.zidian.student.trucks.R;
import com.zidian.study.globa.Constant;
import com.zidian.study.utils.GetUserInfo;
import com.zidian.study.utils.ProgressBarBlackUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.ref.WeakReference;
import java.util.HashMap;

import okhttp3.Call;

/**
 * Created by GongCheng on 2016/12/14.
 */

public class LoadActivity extends AppCompatActivity {
    StaticHandler handler = new StaticHandler(this);

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_load);

    }

    @Override
    protected void onResume() {
        super.onResume();

        handler.sendEmptyMessageDelayed(1 , 2000);
    }

    /**
     * 静态内部类handler
     */
    static class StaticHandler extends Handler {
        WeakReference<LoadActivity> activityWeakReference;

        StaticHandler(LoadActivity activity) {
            activityWeakReference = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            LoadActivity activity = activityWeakReference.get();
            super.handleMessage(msg);
            if (activity != null) {
                switch (msg.what) {
                    case 1:
                        activity.startActivity(new Intent(activity , LoginActivity.class));
                        activity.overridePendingTransition(android.R.anim.fade_in , android.R.anim.fade_out);
                        break;
                }
            }
        }
    }
    private void loginUpdata(String userName , String password) {
        String loginUrl = Constant.URL_GOAL + "shiro/loginStudent";
        HashMap<String, String> map = new HashMap<>();
        map.put("username", GetUserInfo.getStudentId(this));
        map.put("password", password);


        OkHttpUtils.post().url(loginUrl).params(map).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
            }

            @Override
            public void onResponse(String response, int id) {
                ProgressBarBlackUtil.loadCancel();
                try {
                    JSONObject jsonObject = null;
                    jsonObject = new JSONObject(response);
                    int resultCode = jsonObject.getInt("code");

                    if (resultCode == 200) {
                        SharedPreferences sharedPreferences = getSharedPreferences(Constant.USER_INFO_KEY, Context.MODE_PRIVATE);
                        SharedPreferences.Editor editor = sharedPreferences.edit();//获取编辑器
                        editor.putString("account", GetUserInfo.getStudentId(LoadActivity.this));
                        editor.putString("studentId", jsonObject.getString("studentId"));
                        editor.putString("studentName", jsonObject.getString("studentName"));
                        editor.putString("token", jsonObject.getString("token"));
                        editor.putBoolean("isLogin", true);
                        editor.commit();
                        finish();
                    }
                } catch (JSONException e) {

                }

            }
        });
    }
}
